/**
 * 商家服务API
 * 提供商家相关的数据接口
 */

const config = require('./config');

const MerchantAPI = {
  // 获取所有商家列表
  getAllMerchants: function(params = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${config.api.baseURL}/merchants`,
        method: 'GET',
        data: params,
        timeout: config.api.timeout,
        success: function(res) {
          if (res.statusCode === 200 && res.data.success) {
            resolve(res.data);
          } else {
            reject(new Error(res.data.message || '获取商家列表失败'));
          }
        },
        fail: function(err) {
          reject(new Error(config.messages.networkError));
        }
      });
    });
  },

  // 按分类获取商家
  getMerchantsByCategory: function(category, params = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${config.api.baseURL}/merchants/category/${encodeURIComponent(category)}`,
        method: 'GET',
        data: params,
        timeout: config.api.timeout,
        success: function(res) {
          if (res.statusCode === 200 && res.data.success) {
            resolve(res.data);
          } else {
            reject(new Error(res.data.message || '获取商家列表失败'));
          }
        },
        fail: function(err) {
          reject(new Error(config.messages.networkError));
        }
      });
    });
  },

  // 获取商家详情
  getMerchantDetail: function(merchantId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${config.api.baseURL}/merchants/${merchantId}`,
        method: 'GET',
        timeout: config.api.timeout,
        success: function(res) {
          if (res.statusCode === 200 && res.data.success) {
            resolve(res.data);
          } else {
            reject(new Error(res.data.message || '获取商家详情失败'));
          }
        },
        fail: function(err) {
          reject(new Error(config.messages.networkError));
        }
      });
    });
  },

  // 搜索商家
  searchMerchants: function(keyword, params = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${config.api.baseURL}/merchants/search/${encodeURIComponent(keyword)}`,
        method: 'GET',
        data: params,
        timeout: config.api.timeout,
        success: function(res) {
          if (res.statusCode === 200 && res.data.success) {
            resolve(res.data);
          } else {
            reject(new Error(res.data.message || '搜索商家失败'));
          }
        },
        fail: function(err) {
          reject(new Error(config.messages.networkError));
        }
      });
    });
  },

  // 获取商家分类列表
  getMerchantCategories: function() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${config.api.baseURL}/merchants/categories`,
        method: 'GET',
        timeout: config.api.timeout,
        success: function(res) {
          if (res.statusCode === 200 && res.data.success) {
            resolve(res.data);
          } else {
            reject(new Error(res.data.message || '获取商家分类失败'));
          }
        },
        fail: function(err) {
          reject(new Error(config.messages.networkError));
        }
      });
    });
  },

  // 拨打电话
  callPhone: function(phoneNumber) {
    return new Promise((resolve, reject) => {
      wx.makePhoneCall({
        phoneNumber: phoneNumber,
        success: function() {
          resolve('拨打电话成功');
        },
        fail: function(err) {
          reject(new Error('拨打电话失败'));
        }
      });
    });
  },

  // 导航到商家位置
  navigateToLocation: function(latitude, longitude, name) {
    return new Promise((resolve, reject) => {
      wx.openLocation({
        latitude: parseFloat(latitude),
        longitude: parseFloat(longitude),
        name: name,
        scale: 18,
        success: function() {
          resolve('导航成功');
        },
        fail: function(err) {
          reject(new Error('导航失败'));
        }
      });
    });
  }
};

module.exports = MerchantAPI;